from itsdangerous import TimedJSONWebSignatureSerializer as TJWSS

from meiduo_mall import settings


def generate_verify_email_url(user_id):

    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=300)
    token = s.dumps({"user_id": user_id}).decode()
    verify_url = 'http://www.meiduo.site:8000/emails/verification/?token=' + token
    return verify_url


def decode_email_token(token):
    s = TJWSS(secret_key=settings.SECRET_KEY, expires_in=300)

    try:
        user_id = s.loads(token).get('user_id')
        return user_id

    except Exception:
        return None
